/**
 * 
 */
package org.log4jconfig.xml.editor.hyperlink;

import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IType;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.hyperlink.AbstractHyperlinkDetector;
import org.eclipse.jface.text.hyperlink.IHyperlink;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion;
import org.eclipse.wst.sse.core.internal.text.rules.SimpleStructuredTypedRegion;
import org.eclipse.wst.sse.ui.internal.contentassist.ContentAssistUtils;
import org.eclipse.wst.xml.core.internal.parser.regions.AttributeValueRegion;
import org.log4jconfig.xml.editor.utils.JdtUtils;
import org.log4jconfig.xml.editor.utils.Logger;
import org.log4jconfig.xml.editor.utils.StructuredDocumentRegionUtils;

/**
 * @author Xi
 * 
 */
@SuppressWarnings("restriction")
public class ClassTypeHyperlinkDetector extends AbstractHyperlinkDetector {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.text.hyperlink.IHyperlinkDetector#detectHyperlinks(
	 * org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion,
	 * boolean)
	 */
	public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
		try {
			IStructuredDocumentRegion structuredDocumentRegion = ContentAssistUtils.getStructuredDocumentRegion(
					textViewer, region.getOffset());
			ITextRegion attributeValueRegion = structuredDocumentRegion.getRegionAtCharacterOffset(region.getOffset());
			if (attributeValueRegion instanceof AttributeValueRegion) {
				String tagName = StructuredDocumentRegionUtils.extractTagName(structuredDocumentRegion);
				String attributeName = StructuredDocumentRegionUtils.extractAttributeName(structuredDocumentRegion,
						(AttributeValueRegion) attributeValueRegion);
				// check tagName + attributeName regions to avoid useless
				// processings
				if ((("appender".equals(tagName) || "layout".equals(tagName)) && "class".equals(attributeName))
						|| ("logger".equals(tagName) && "name".equals(attributeName))) {
					IJavaProject javaProject = JdtUtils.getJavaProject(structuredDocumentRegion.getParentDocument());
					String attributeValue = StructuredDocumentRegionUtils.extractAttributeValue(
							structuredDocumentRegion, region.getOffset());
					if (attributeValue != null && attributeValue.length() > 0) {
						IType type = JdtUtils.getTypeFromCurrentProject(javaProject, attributeValue);
						if (type != null && type.getElementType() == IJavaElement.TYPE) {
							IRegion hyperlinkRegion = new SimpleStructuredTypedRegion(structuredDocumentRegion
									.getStartOffset()
									+ attributeValueRegion.getStart() + 1, attributeValue.length(),
									attributeValueRegion.getType());
							return new IHyperlink[] { new JavaElementHyperlink(hyperlinkRegion, type) };
						} else {
							Logger.warn("Unable to find type '" + attributeValue + "' in project classpath");
						}
					}
				}
			}
		} catch (Exception e) {
			Logger.error("Failed to detect hyperlink: " + e.getMessage(), e);
		}
		return null;

	}

}
